package com.games.ecoship.client;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import com.games.ecoship.EcoShipConstants;

public class ClickListener extends MouseAdapter implements EcoShipConstants {

	public void mouseClicked(MouseEvent e) {
		
		Cell cellClicked          = ((Cell) e.getSource());
		EcoShipClient playerRef   = cellClicked.getReference(); 
		
		// After the game has been setup, additional clicks allow the player to attack the enemy.
		if (!playerRef.settingUp()) {
			
			// If the cell clicked on is blank, and if it is currently the player's turn, then
			// the cell will be selected for the attack.
			if (cellClicked.getToken() == EMPTY_SPOT && playerRef.isMyTurn()) {
				
				playerRef.setTurn(false);
				playerRef.setColSelected(cellClicked.getCol());
				playerRef.setRowSelected(cellClicked.getRow());
				playerRef.setCellSelected(cellClicked);
				playerRef.setWaitingStatus(false);
			}
		}
		// Otherwise, when the player clicks on his own board, one of his ships will be placed.
		else {
			System.out.println(cellClicked.getPlayerNum());
			if (cellClicked.getPlayerNum() == playerRef.getPlayerNum()) {
				setShipSpaces(cellClicked, playerRef);
			}
			else {
				// TODO: Inform the player that they can't click on that cell to set their ship;
				//       they must select their own tiles.
				System.out.println("You cannot select the enemy board to place your ships.");
			}
		}
	}

	/**
	 * Gets the current ship to be set, and based on its size and orientation, it will
	 * determine if enough spaces are open so that this ship can be places and occupy 
	 * all of the spaces to the right or below from where the clicked cell is located.
	 * It will then procede to fill those spaces accordingly.
	 * 
	 * @param cellClicked
	 * @param playerRef
	 * @return
	 */
	private void setShipSpaces(Cell cellClicked, EcoShipClient playerRef) {
		
		Boolean spacesOpen   = true;
		Ship ship            = playerRef.getShip(playerRef.getNumShipsSet());
		Cell playerBoard[][] = playerRef.getPlayerBoard(); 
		
		// If the orientation of the ship is HORIZONTAL
		if (ship.getOrientation() == Ship.HORIZONTAL) {
			
			// If there are enough cells on the board to the right from this cell, 
			// the ship can possibly be placed there
			if (cellClicked.getRow() + ship.getSize() < NUM_ROWS) {
				
				// Now, each cell in the path must be checked to ensure that there are no
				// closed spaces (occupied by other ships)
				for (int size=0; size < ship.getSize(); size++) {
					if (playerBoard[size + cellClicked.getRow()][cellClicked.getCol()].getToken() != EMPTY_SPOT) {
						spacesOpen = false;
						break;
					}
				}
			}
			else
				spacesOpen = false;
		}
		// If the orientation of the ship is VERTICAL
		else {
			
			// If there are enough cells on the board below this cell, 
			// the ship can possibly be placed there
			if (cellClicked.getCol() + ship.getSize() < NUM_COLS) {
				
				// Now, each cell in the path must be checked to ensure that there are no
				// closed spaces (occupied by other ships)
				for (int size=0; size < ship.getSize(); size++) {
					if (playerBoard[cellClicked.getRow()][size + cellClicked.getCol()].getToken() != EMPTY_SPOT) {
						spacesOpen = false;
						break;
					}
				}
			}
			else
				spacesOpen = false;
		}
		
		// Fill the cells in which the ship can occupy
		if (spacesOpen) {
			for (int size=0; size < ship.getSize(); size++) {
				
				// If the orientation of the ship is HORIZONTAL
				if (ship.getOrientation() == Ship.HORIZONTAL) {
					
					// Set the cell to be the number of this ship
					playerBoard[size + cellClicked.getRow()][cellClicked.getCol()].setToken(Integer.toString(ship.getShipNum()).charAt(0));
				}
				// If the orientation of the ship is VERTICAL
				else {
					
					// Set the cell to be the number of this ship
					playerBoard[cellClicked.getRow()][size + cellClicked.getCol()].setToken((char)ship.getShipNum());
				}
			}
			playerRef.shipSet();
		}
		// Inform the player that the ship cannot be placed at the clicked cell.
		else {
			// TODO: Make this message appear on the status label on the client.
			System.out.println("Ship #" + ship.getShipNum() + " cannot be placed at row[" 
								+ cellClicked.getRow() + "]col[" + cellClicked.getCol() + "].");
		}
	}
}
